<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>

    </style>
</head>
<body>
    <div class="wrap">
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<!-- <script type="text/javascript" src="../script/socket.js"></script> -->
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">

    var timer; // 定义定时器
    // var GoogleMap; // 定义谷歌地图
    var socketManager;

    apiready = function(){
        var user = $api.getStorage('user_data');
        socketManager = api.require('socketManager');
        var connectionType = api.connectionType;
        if(user && user.id && 'none' != connectionType){
            // accept_socket('user_id'+user.id);
            fnConnectSocket();
        }

        api.addEventListener({
            name: 'reloadSocketEvent'
        }, function(ret, err){
            if( ret ){
                fnConnectSocket();
                // window.location.reload();
            }
        });

        api.addEventListener({
            name: 'reloadSocketEvent2'
        }, function(ret, err){
            if( ret ){
                fnCloseSocket();
                // window.location.reload();
            }
        });

        api.addEventListener({
            name: 'online'
        }, function(ret, err){
            if( ret ){
                fnConnectSocket();
                // window.location.reload();
            }
        });
        api.addEventListener({
            name: 'offline'
        }, function(ret, err){
            if( ret ){
                fnCloseSocket();
                // window.location.reload();
            }
        });



        api.addEventListener({
            name:'resume'
        }, function(ret, err){
            if(user){
                // 获取地理位置
            }
        });
    };

    function fnConnectSocket () {
        user = $api.getStorage('user_data');
        if(!user){
            return;
        }
        socketManager.createSocket({
            host: '13.250.39.107',
            port: 8282
        }, function(ret, err) {
            if(ret){
                if (ret.state == 103) {
                    if(ret.sid){
                        $api.setStorage('user_sid', ret.sid);
                    }
                    if(ret.data){
                        var data = JSON.parse(ret.data);
                        if (data.type == 'init') {
                            api.ajax({
                                url: rootPath + '/workerMan/bind',
                                method: 'post',
                                data: {
                                    values: {
                                        uid: 'user_id'+user.id, // 这个需要传入真实的uid并加项目前缀
                                        client_id: data.client_id,
                                    },

                                }
                            });
                        } else if (data.flag == 'bit_user') {
                            receive_fn(data);
                        }
                    }
                }
            }
        });
    };


    function fnCloseSocket () {
        socketManager.closeSocket({
            sid: $api.getStorage('user_sid'),
        }, function(ret, err) {
            if (ret.status) {

            } else {

            }
        });
    };

    //监听推送具体函数
    function receive_fn(result) {
        console.log(result);
        var data = [];
        if(result && result.msg){
            var data = JSON.parse(result.msg);
        }
        // alert(data.todo);
        if(data.todo == 'appoint_order'){
            var order_detail = JSON.parse(data.order_detail);
        }
        // 商家确认用户的订单
        if(data.todo == 'confirm_user_order') {
            api.sendEvent({
                name: 'orderChangeEvent',
                extra: {
                }
            });
        }else if (data.todo == 'paydollar_success'){
            if(data.type == 0){
                // 支付完成打开支付页面 - 暂时sendevent，没空修改先
                api.sendEvent({
                    name: 'paydollarSuccess',
                    extra: {
                        oid: data.oid
                    }
                });
            }else{
                // 支付包裹完成，弹出查看订单dialog
                var jsfun = 'openSuccessFrame('+data.oid+');';
                api.execScript({
                    name: 'paydollar',
                    script: jsfun
                });
            }
            // 改变订单状态
            api.sendEvent({
                name: 'orderChangeEvent',
            });
        }else if(data.todo == 'paydollar_cancel') {
            // 支付取消需要关闭页面
            var jsfun = 'paydollarCancel();';
            api.execScript({
                name: 'paydollar',
                script: jsfun
            });
        }else if(data.todo == 'staff_catch_order') {
            // 骑手抢了用户的订单
            api.sendEvent({
                name: 'orderChangeEvent',
                extra: {
                }
            });
        }else if(data.todo == 'shop_scan_staff_order_success') {
            // 商家扫描骑手的订单确认骑手已经取货
            api.sendEvent({
                name: 'orderChangeEvent',
                extra: {
                }
            });
        }

    }


</script>
</html>
